<?php
function get_post_script() {
    global $_G;
    $s = '<script>';
    $s .= 'var pub_credit=' . getCreditIndex() . ';' . "\n";
    $s .= 'var def=' . $_G['cache']['plugin']['huanl_reward']['default'] . '</script>';
    $s .= <<<s
    <script>
        var re= document.getElementById('rewardprice');
        var tmpStr=re.nextSibling.data;
        re.nextSibling.data=tmpStr.substr(tmpStr.indexOf(','));
        //create select
        var select=document.createElement('select');
        select.id='credit-select';
        select.name='credit';
        re.parentElement.insertBefore(select,re.nextSibling);
        for(key in pub_credit){
            var option=document.createElement('option');
            option.value=key;
            option.innerText=pub_credit[key];
            select.appendChild(option);
        }
        //create type label
        var ctype=document.createElement('span');
        ctype.id='credit-text';
        re=document.getElementById('realprice');
        re.parentElement.insertBefore(ctype,re.nextElementSibling);
        re.parentElement.removeChild(re.nextSibling);
        //default value
        select.value=def;
        change_credit(def);
        select.onchange=change_credit;
        // change credit,show number
        function change_credit(){
            ctype.innerText=" "+pub_credit[select.value]
            //fetch credit number
            var tmp=document.getElementsByClassName('mtn xg1');
            if(tmp.length<=0){
                return;
            }
            tmp=tmp[0];
            var http = new XMLHttpRequest();
            http.open('get', 'plugin.php?id=huanl_reward:api&action=get_credit_number&credit_index=' + select.value);
            http.onreadystatechange = function () {
                if (http.status == 200 && http.readyState == 4) {
                    var tmpStr=tmp.innerText;
                    tmp.innerHTML=tmpStr.substr(0,tmpStr.indexOf(','))+' , 您有 '+http.responseText+' '+pub_credit[select.value];
                }
            }
            http.send();
        }
    </script>
s;

    return $s;
}

function getCreditIndex() {
    global $_G;
    $arr = explode(',', $_G['cache']['plugin']['huanl_reward']['credit_index']);
    $ret = [];
    foreach ($arr as $value) {
        $ret[$value] = $_G['setting']['extcredits'] [$value]['title'];
    }
    return json_encode($ret,JSON_UNESCAPED_UNICODE);
}

function get_list_script() {
    $ret = '<script>var pub_credit=' . getCreditIndex() . ';' . "\n</script>";
    $ret .= <<<ret
    <script>
    function scanning(){
        var list=document.getElementById('threadlisttableid').getElementsByTagName('tbody');
        var postData="";
        for (var key=0;key<list.length;key++){
            var xi=list[key].getElementsByClassName('xi1');
            if(xi==undefined || xi.length<=0)continue;
            xi=xi[0];
            if (xi.innerText.indexOf('悬赏')==-1 || xi.className.indexOf('over')>0)continue; 
            var tid=list[key].id;
            tid=tid.substr(13);
            postData+='tid['+key+']='+tid+'&';
            console.log(list[key]);
            xi.className+=' over';
        }
        var ax=new Ajax('JSON');
        ax.post('plugin.php?id=huanl_reward:api&action=get_credit_type',postData,function(json) {
            for(key in json){
              var xw=list[key].getElementsByClassName('xw1')[0];
              var tmp=document.createElement("span");
              tmp.innerText=" "+pub_credit[json[key]]+"]";
              xw.parentNode.insertBefore(tmp,xw.nextSibling);
              xw.nextSibling.nextSibling.remove();
              
            }
        });
       
        console.log(postData);
    }
    scanning();
    </script>
ret;

    return $ret;
}

?>